using UnityEngine;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System;
using System.Threading;
using System.Text;
using System.IO;

using UnityEngine.UI;
using UnityEngine.EventSystems;

/// <summary>
/// 飞船的功能面板
/// </summary>
public enum FunctionPanelType : int
{
    updatePanel,
    transformPanel,
    advancePanel,
    restorePanel,
    compoundPanel,
}

public enum EnumTouchEventType
{
    OnClick,
    OnDoubleClick,
    OnDown,
    OnUp,
    OnEnter,
    OnExit,
    OnSelect,
    OnUpdateSelect,
    OnDeSelect,
    OnDrag,
    OnDragEnd,
    OnDrop,
    OnScroll,
    OnMove,
}

/// <summary>
/// 挑战的结果
/// </summary>
public enum ChallengeResultType
{
    None,
    Win,
    Failure,
}

/// <summary>
/// 查看规则类型
/// </summary>
public enum ViewRuleType
{
    None,
    FormationRule,
    AllianceViewRules,
}

public delegate void OnTouchEventHandle();

public class ComFuncAttForUI
{
    public static ViewRuleType thisViewRuleType = ViewRuleType.None;

    public static ChallengeResultType currentChallengeResultType = ChallengeResultType.None;

    static ComFuncAttForUI instance;
    //public delegate void OnTouchEventHandle(GameObject _listener, object _args, params object[] _params);

    //飞船配件映射信息持久化类
    static int temThousandNum = 10000;
    public static int TemThousandNum { get { return temThousandNum; } set { temThousandNum = value; } }

    //飞船配件映射信息持久化类
    static int lastFightingCapacity = 0;
    public static int LastFightingCapacity { get { return lastFightingCapacity; } set { lastFightingCapacity = value; } }

    public static int maxAdvanceLevel = 15;//表里的最大进阶等级数  15
    public static int maxFittingAdvanceLevel = 14;//表里的最大进阶等级数  15

    static int maxUpdateCostLevelNum;//飞船升级费用最高的等级数
    public static int MaxUpdateCostLevelNum { get { return maxUpdateCostLevelNum; } set { maxUpdateCostLevelNum = value; } }

    //配件属性面板的属性值 中转值
    static float tempfittingAttributePanelShowNum;
    public static float TempFittingAttributePanelShowNum { get { return tempfittingAttributePanelShowNum; } set { tempfittingAttributePanelShowNum = value; } }

    //配件中飞船战斗力的中转值
    static int tempFightingCapacityShowForFitting;
    public static int TempFightingCapacityShowForFitting { get { return tempFightingCapacityShowForFitting; } set { tempFightingCapacityShowForFitting = value; } }

    //主界面判断是不是拖动
    public static bool isDraggingForMain = false;

    public static int CurrentChallengeLevel = 0;

    public static int MaxChallengeLevel = 15;//挑战的最高层数

    public static bool IsChallengeShow = false;

    //挑战界面的自动挑战
    public static bool IsAutoForChallenge = false;
    //挑战界面 是不是从战斗回来
    public static bool IsGoBackChallenge = false;
    //挑战界面 判断是不是从阵型或 排行回来
    public static bool IsOtherBackToChallenge = false;

    public static int LastChallengeLevel = 1;//无尽挑战的上一关
    public static int LastContributeNum = 0;//上一关的总奖励  贡献 
    public static int LaststarCurrencyNum = 0;//上一关的总奖励  星币
    public static int LastStarStoneNum = 0;//上一关的总奖励  神石

    public static int currentThFittingID = 0;//当前点击的配件的ID

    public static int IsHasSetAChallenge;

    // 飞船改造 最高数值判定
    public static Dictionary<string, float> AShipTransformMaxNums = new Dictionary<string, float>();

    /// <summary>
    /// 根据ID获得 挑战排行榜奖励 字典
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public static float GetTransformMaxNumsByQuality(string quality)
    {
        if (AShipTransformMaxNums.ContainsKey(quality))
        {
            return AShipTransformMaxNums[quality];
        }
        return 0;
    }

    /// <summary>
    /// 按钮点击 的变大
    /// </summary>
    /// <param name="go"></param>
    public static void SetButtonScale(GameObject go, int i = 0)
    {
        UITriggerListener listener = UITriggerListener.Get(go);
        Transform changeTrans;
        if (i == 1)
        {
            changeTrans = go.transform.parent;
        }
        else
        {
            changeTrans = go.transform;
        }

        listener.SetEventHandle(EnumTouchEventType.OnDown, () =>
        {
            changeTrans.localScale = new Vector3(1.05f, 1.05f, 1.05f);
        });
        listener.SetEventHandle(EnumTouchEventType.OnUp, () =>
        {
            changeTrans.localScale = new Vector3(1f, 1f, 1f);
        });

    }

    /// <summary>
    /// 改变特效的层级
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="Order"></param>
    public static void ControlEffectsOrder(GameObject parent, int orderPen, int orderAll)
    {
        Transform[] children = parent.GetComponentsInChildren<Transform>(true);
        foreach (Transform child in children)
        {
            if (child.gameObject.GetComponent<ParticleSystem>() != null)
            {
                if (child.gameObject.name == "pen4" || child.gameObject.name == "pen1" || child.gameObject.tag == "effectDown")
                {
                    child.gameObject.GetComponent<ParticleSystem>().GetComponent<Renderer>().sortingOrder = orderPen;
                }
                else
                {
                    child.gameObject.GetComponent<ParticleSystem>().GetComponent<Renderer>().sortingOrder = orderAll;
                }
            }
        }
    }

    /// <summary>
    /// 恭喜获得 item 的特效选择
    /// </summary>
    /// <param name="go"></param>
    /// <param name="quality"></param>
    public static void GetIconBackEffForHave(GameObject go, string quality)
    {
        GameObject effectlv = go.transform.Find("award/effect_ui_pinzhi_biankuang_lv").gameObject;
        GameObject effectlan = go.transform.Find("award/effect_ui_pinzhi_biankuang_lan").gameObject;
        GameObject effectzi = go.transform.Find("award/effect_ui_pinzhi_biankuang_zi").gameObject;
        GameObject effectcheng = go.transform.Find("award/effect_ui_pinzhi_biankuang_cheng").gameObject;
        effectlv.gameObject.SetActive(false);
        effectlan.gameObject.SetActive(false);
        effectzi.gameObject.SetActive(false);
        effectcheng.gameObject.SetActive(false);
        switch (quality)
        {
            case "2": effectlv.gameObject.SetActive(true);
                break;
            case "3": effectlan.gameObject.SetActive(true);
                break;
            case "4": effectzi.gameObject.SetActive(true);
                break;
            case "5": effectcheng.gameObject.SetActive(true);
                break;
            case "6": break;
        }
    }

    /// <summary>
    /// 必杀说明的位置
    /// </summary>
    public static Vector3 GetOnePositionInKills(int mEmbIndex)
    {
        Vector3 one = new Vector3();

        switch (mEmbIndex)
        {
            case 1:
                one = new Vector3(-241, 174, 0);
                break;
            case 2:
                one = new Vector3(-241, -70, 0);
                break;
            case 3:
                one = new Vector3(-241, -284, 0);
                break;
            default:
                break;
        }
        return one;
    }

    /// <summary>
    /// 设置飞船品质的底色
    /// </summary>
    /// <param name="go"></param>
    /// <param name="mTempShipInfo"></param>
    public static void ShipColorLineShow(int mQualityNum, Image imageBg)
    {
        switch (mQualityNum)
        {
            case 3:
                imageBg.overrideSprite = Resources.Load("UI_Resources/apacestationnew/蓝色品质", typeof(Sprite)) as Sprite;
                break;
            case 2:
                imageBg.overrideSprite = Resources.Load("UI_Resources/apacestationnew/绿色品质", typeof(Sprite)) as Sprite;
                break;
            case 5:
                imageBg.overrideSprite = Resources.Load("UI_Resources/apacestationnew/橙色品质", typeof(Sprite)) as Sprite;
                break;
            case 4:
                imageBg.overrideSprite = Resources.Load("UI_Resources/apacestationnew/紫色品质", typeof(Sprite)) as Sprite;
                break;
            default:
                break;
        }
    }


    /// <summary>
    /// 刷新布阵信息类
    /// </summary>
    public static void SentAllInfoForEmbattles()
    {
        Message msg = new Message(MessageType_UI.Sent_ChangeAllEmbattleInfo, instance, null);
        MessageCenter.Instance.SendMessage(msg);
    }

    /// <summary>
    /// 配件界面  飞船红点的更新
    /// </summary>
    public static void SentFittingRedDotToChange()
    {
        Message msg = new Message(MessageType_UI.Sent_FittingRedDotShow, instance, null);
        MessageCenter.Instance.SendMessage(msg);
    }

    /// <summary>
    /// 关闭右面的按钮的红点
    /// </summary>
    public static void SentCloseAllRedForRightButton()
    {
        Message msg = new Message(MessageType_UI.Sent_CloseAllRedForRightButton, instance, null);
        MessageCenter.Instance.SendMessage(msg);
    }

    /// <summary>
    /// 刷新 空间站 飞船上的红点
    /// </summary>
    public static void SentReSetRedForSpaceInAir()
    {
        Message msg = new Message(MessageType_UI.Sent_ReSetRedForSpaceInAir, instance, null);
        MessageCenter.Instance.SendMessage(msg);
    }

    /// <summary>
    /// 布阵遮罩的开启
    /// </summary>
    public static void SentForMaskTopForIsClickOpen()
    {
        Message msg = new Message(MessageType_UI.Sent_Open_MaskTopForIsClickForEmbattle, instance, null);
        MessageCenter.Instance.SendMessage(msg);
    }

    /// <summary>
    /// 布阵遮罩的关闭
    /// </summary>
    public static void SentForMaskTopForIsClickClose()
    {
        Message msg = new Message(MessageType_UI.Sent_Close_MaskTopForIsClickForEmbattle, instance, null);
        MessageCenter.Instance.SendMessage(msg);
    }

    /// <summary>
    /// 设置子物体和父物体的对应
    /// </summary>
    /// <param name="child"></param>
    /// <param name="parent"></param>
    public static void SetChildForParent(GameObject child, GameObject parent)
    {
        child.transform.SetParent(parent.transform);
        child.transform.localPosition = Vector3.zero;
        child.transform.localScale = Vector3.one;
        child.transform.localEulerAngles = Vector3.zero;
    }

    /// <summary>
    /// 通过序列号知道配件类型
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static string GetFittingPropType(int index)
    {
        switch (index)
        {
            case 1:
                return "fasheqi";
            case 2:
                return "zhuangjia";
            case 3:
                return "tuijinqi";
            case 4:
                return "yinqing";
            case 5:
                return "leida";
            case 6:
                return "qudongqi";
            default:
                break;
        }
        return null;
    }

    /// <summary>
    /// 设置配件的空图标
    /// </summary>
    public static void SetFittingImageS(int index, Image fittingImage)
    {
        switch (index)
        {
            case 1:
                fittingImage.overrideSprite = Resources.Load("UI_Resources/FitingShow/发射器-hui", typeof(Sprite)) as Sprite;
                break;
            case 2:
                fittingImage.overrideSprite = Resources.Load("UI_Resources/FitingShow/护盾-hui", typeof(Sprite)) as Sprite;
                break;
            case 3:
                fittingImage.overrideSprite = Resources.Load("UI_Resources/FitingShow/推进器-hui", typeof(Sprite)) as Sprite;
                break;
            case 4:
                fittingImage.overrideSprite = Resources.Load("UI_Resources/FitingShow/引擎-hui", typeof(Sprite)) as Sprite;
                break;
            case 5:
                fittingImage.overrideSprite = Resources.Load("UI_Resources/FitingShow/雷达-hui", typeof(Sprite)) as Sprite;
                break;
            case 6:
                fittingImage.overrideSprite = Resources.Load("UI_Resources/FitingShow/驱动器-hui", typeof(Sprite)) as Sprite;
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 根据品质显示配件的框
    /// </summary>
    public static void SetFittingLineImage(string propQuality, Image gunItem)
    {
        switch (propQuality)
        {
            case "2":
                gunItem.overrideSprite = Resources.Load("UI_Resources/Common_Common/icon框_绿", typeof(Sprite)) as Sprite;
                break;
            case "3":
                gunItem.overrideSprite = Resources.Load("UI_Resources/Common_Common/icon框_蓝", typeof(Sprite)) as Sprite;
                break;
            case "4":
                gunItem.overrideSprite = Resources.Load("UI_Resources/Common_Common/icon框_紫", typeof(Sprite)) as Sprite;
                break;
            case "5":
                gunItem.overrideSprite = Resources.Load("UI_Resources/Common_Common/icon框_橙", typeof(Sprite)) as Sprite;
                break;
            case "6":///红色图片不对,以后再出
                gunItem.overrideSprite = Resources.Load("UI_Resources/Common_Common/icon框_紫", typeof(Sprite)) as Sprite;
                break;
            default:
                gunItem.overrideSprite = Resources.Load("UI_Resources/Common_Common/icon框_蓝", typeof(Sprite)) as Sprite;
                break;
        }
        gunItem.SetNativeSize();
    }

    public static void GetIconBackEff(GameObject go, string quality)
    {
        GameObject effectlv = go.transform.Find("gunItem/effect_ui_pinzhi_biankuang_lv").gameObject;
        GameObject effectlan = go.transform.Find("gunItem/effect_ui_pinzhi_biankuang_lan").gameObject;
        GameObject effectzi = go.transform.Find("gunItem/effect_ui_pinzhi_biankuang_zi").gameObject;
        GameObject effectcheng = go.transform.Find("gunItem/effect_ui_pinzhi_biankuang_cheng").gameObject;
        effectlv.gameObject.SetActive(false);
        effectlan.gameObject.SetActive(false);
        effectzi.gameObject.SetActive(false);
        effectcheng.gameObject.SetActive(false);
        switch (quality)
        {
            case "2": effectlv.gameObject.SetActive(true);
                break;
            case "3": effectlan.gameObject.SetActive(true);
                break;
            case "4": effectzi.gameObject.SetActive(true);
                break;
            case "5": effectcheng.gameObject.SetActive(true);
                break;
            case "6": break;
        }
    }

    /// <summary>
    /// 通过品质返回用来改变字体的字符串信息
    /// </summary>
    /// <param name="propQuality"></param>
    /// <returns></returns>
    public static string GetStringForTextColor(string propQuality)
    {
        switch (propQuality)
        {
            case "2":
                return "<color=#09FF0BFF>";
            case "3":
                return "<color=#0000FFFF>";
            case "4":
                return "<color=#FF00F6FF>";
            case "5":
                return "<color=#FF8A00FF>";
            case "6":///红色图片不对,以后再出
                return "<color=#FF0000FF>";
            default:
                return null;
        }
    }

    /// <summary>
    /// 通过品质返回用来改变字体的字符串信息
    /// </summary>
    /// <param name="propQuality"></param>
    /// <returns></returns>
    public static string GetStringForColorConstShow(string propQuality)
    {
        switch (propQuality)
        {
            case "2":
                return "绿色";
            case "3":
                return "蓝色";
            case "4":
                return "紫色";
            case "5":
                return "橙色";
            case "6":///红色图片不对,以后再出
                return "红色";
            default:
                return null;
        }
    }

    /// <summary>
    /// 解析信息的方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="msg"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static T GetValueFromMsg<T>(Message msg, string key) //where T : object
    {
        if (msg != null && msg[key] != null)
        {
            object val = msg[key];
            return (T)val;
        }

        return default(T);
    }

    /// <summary>
    /// 为对象添加脚本
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="go"></param>
    public static void GetControll<T>(GameObject go) where T : Component
    {
        T t = go.transform.GetComponent<T>();
        if (t == null)
        {
            go.gameObject.AddComponent<T>();
        }
    }

    /// <summary>
    /// 为对象添加脚本，并返回
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="go"></param>
    public static T GetControllAndReturn<T>(GameObject go) where T : Component
    {
        T t = go.transform.GetComponent<T>();
        if (t == null)
        {
            go.gameObject.AddComponent<T>();
        }
        return go.transform.GetComponent<T>();
    }

    /// <summary>
    /// 删除对象上面的指定组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="go"></param>
    public static void DestroyComponentByName<T>(GameObject go) where T : Component
    {
        T t = go.transform.GetComponent<T>();
        if (t != null)
        {
            GameObject.Destroy(t);
        }
    }

    /// <summary>
    /// 设置为统一的 tag
    /// </summary>
    public static void SetTag(GameObject go, string tagShow)
    {
        Transform[] children = go.gameObject.GetComponentsInChildren<Transform>(true);
        foreach (Transform child in children)
        {
            child.tag = tagShow;
        }
    }

    /// 返回点击对象
    /// </summary>
    /// <returns>点击对象Tag</returns>
    public static GameObject OnePointColliderObject()
    {
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

        if (results.Count > 0)
        {
            return results[0].gameObject;
        }
        else
            return null;
    }

    /// <summary>
    /// 获得中文
    /// </summary>
    /// <returns></returns>
    public static string GetChineseForComposite(string key)
    {
        string mChinese = null;

        switch (key)
        {
            case "Attack":
                mChinese = "攻击";
                break;
            case "Defense":
                mChinese = "防御";
                break;
            case "Life":
                mChinese = "生命";
                break;
            case "Crit":
                mChinese = "暴击";
                break;
            case "Dodge":
                mChinese = "闪避";
                break;
            case "Speed":
                mChinese = "速度";
                break;
            default:
                break;
        }

        return mChinese;
    }

    public static string GetChineseForCompositeForFitting(string key)
    {
        string mChinese = null;

        switch (key)
        {
            case "1":
                mChinese = "攻击";
                break;
            case "2":
                mChinese = "防御";
                break;
            case "3":
                mChinese = "生命";
                break;
            case "4":
                mChinese = "攻击";
                break;
            case "5":
                mChinese = "防御";
                break;
            case "6":
                mChinese = "生命";
                break;
            default:
                break;
        }

        return mChinese;
    }

    public static string GetChineseForCompositeForFittingAllAll(string key)
    {
        string mChinese = null;

        switch (key)
        {
            case "1":
                mChinese = "攻击";
                break;
            case "2":
                mChinese = "防御";
                break;
            case "3":
                mChinese = "生命";
                break;
            case "4":
                mChinese = "暴击";
                break;
            case "5":
                mChinese = "闪避";
                break;
            default:
                break;
        }

        return mChinese;
    }

    public static int canvasNum = 4;//场景里canvas的数量        mGameObject.transform.childCount
    /// <summary>
    /// 获得场景里的 Canvas
    /// </summary>
    /// <returns></returns>
    public static GameObject GetCanvasForScene(object _uiParams)
    {
        GameObject canvas = GameObject.Find("Canvas");
        if (_uiParams == null)
        {
            return canvas;
        }
        else
        {
            int index = int.Parse(_uiParams.ToString());
            //加个判断 类型
            if (index >= 1 || index <= (canvasNum - 1))
            {
                canvas = GameObject.Find("Canvas" + _uiParams);
            }
            else return GameObject.Find("Canvas" + (canvasNum - 1));
        }
        return canvas;
    }
    


    /// <summary>
    /// 查找子物体并返回 Transform
    /// </summary>
    /// <param name="check"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static Transform GetTransform(Transform check, string name)
    {
        foreach (Transform child in check)
        {
            if (child.name == name)
            {
                //要做的事
                return child;
            }
        }
        return null;
    }
}
